package com.pocketpets.ui;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.util.Log;

import com.pocketpets.main.PocketPets;

public class LoadingScreen extends BaseGameActivity implements  IOnSceneTouchListener{

	// ================================================================
	// Constants
	// ================================================================

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 320;
	
	// ================================================================
	// Fields
	// ================================================================
	protected Engine engine; 
	private Camera mCamera;
	private Texture mTexture;
	private TextureRegion mPetTextureRegion;
	private Sprite mPet;
	
	
	public Engine onLoadEngine() {
		this.mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.mCamera));
	}

	public void onLoadResources() {
		String pic = "gfx/theydream.png";
		//Bundle bundle=getIntent().getExtras();
		//pic="gfx/"+(String) bundle.get("pic");
		// TODO Auto-generated method stub
		this.mTexture = new Texture(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mPetTextureRegion = TextureRegionFactory.createFromAsset(
				this.mTexture, this, pic, 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mTexture);
	}

	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		final Scene scene = new Scene(1);
		// scene.setBackgroundEnabled(false);
		scene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f, 0.0f));
//		scene.registerTouchArea(mPet);
		scene.setOnAreaTouchTraversalFrontToBack();
		//this.mScrollDetector = new SurfaceScrollDetector(this);
		//this.mScrollDetector.setEnabled(true);
		final int centerX = (CAMERA_WIDTH - this.mPetTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mPetTextureRegion.getHeight()) / 2;
		mPet = new Sprite(centerX, centerY, this.mPetTextureRegion);
		
		
		
		scene.setOnSceneTouchListener(this);
//				new IOnAreaTouchListener(){
//			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea arg1,
//					float pTouchAreaLocalX, float pTouchAreaLocalY) {
//				Log.i("catchingcatching","ccccccccccc");
//				startActivity(new Intent(CatchActivity.this, CaughtPets.class));
//				return false;
//			}});
		
		
	//	{

//			@Override
//			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
//					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
//				Log.i("catchingcatching","ccccccccccc");
//				startActivity(new Intent(CatchActivity.this, CaughtPets.class));
//				return true;
//			}

//		};
		scene.getLastChild().attachChild(mPet);

		scene.setTouchAreaBindingEnabled(true);
		mPet.registerEntityModifier(new MoveModifier(30, 0, CAMERA_WIDTH
				- mPet.getWidth(), 0, CAMERA_HEIGHT - mPet.getHeight()));
		scene.getLastChild().attachChild(mPet);

		return scene;
	}

	public void onLoadComplete() {
		// TODO Auto-generated method stub
	}


	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		Log.i("TTTTTTTTTTTT","TouchTouchTouchTouchs");
		startActivity(new Intent(LoadingScreen.this, PocketPets.class));
		this.finish();
		//this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
		
//		Toast.makeText(getBaseContext(), "aaaaa", Toast.LENGTH_SHORT);
//		mPet.onAreaTouched(pSceneTouchEvent, pSceneTouchEvent.getX(),
//				pSceneTouchEvent.getY());
		return false;
	}

//	public void onScroll(ScrollDetector pScollDetector, TouchEvent pTouchEvent,
//			float pDistanceX, float pDistanceY) {
//		Log.i("Scolling", "Scroll {x:"+pDistanceX+", y: "+pDistanceY+"}");  
//		// TODO Auto-generated method stub
//		
//	}



	

}
